package com.cstp.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cstp.entity.VolunteerReport;
import com.cstp.manage.WxMpManage;
import com.cstp.web.req.VolunteerReportReq;
import com.cstp.web.res.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author watts
 */
@RestController
@RequestMapping("")
@Slf4j
public class VolunteerReportController {

    @Resource
    private WxMpManage wxMpManage;

    @PostMapping("/report/detail/{id}")
    public Resp<VolunteerReport> detail(@PathVariable Long id) {
        return Resp.ok(new VolunteerReport().selectById(id));
    }

    @PostMapping("/report/approved/{id}/{state}")
    public Resp<Boolean> approved(@PathVariable Long id, @PathVariable int state) {
        VolunteerReport v = new VolunteerReport();
        v.setId(id);
        v.setState(state);
        v.setModifyTime(LocalDateTime.now());
        VolunteerReport volunteerReport = new VolunteerReport().selectById(id);
        wxMpManage.sendReportApprovedMsg(volunteerReport.getUserId(), volunteerReport.getUname(), volunteerReport.getContent());
        return Resp.ok(v.updateById());
    }

    @PostMapping("/report/list")
    public Resp<IPage<VolunteerReport>> list(@RequestBody VolunteerReportReq param) {
        IPage<VolunteerReport> page = new Page<>(param.getPageNum(), param.getPageSize());
        VolunteerReport report = new VolunteerReport();
        page = report.selectPage(page, new QueryWrapper<>(report)
                .like(Objects.nonNull(param.getName()), "uname", param.getName())
                .like(Objects.nonNull(param.getPhone()), "phone", param.getPhone())
                .like(Objects.nonNull(param.getIdCard()), "id_card", param.getIdCard())
                .eq(Objects.nonNull(param.getState()), "state", param.getState())
                .orderByDesc("create_time", "modify_time"));
        return Resp.ok(page);
    }

    @PostMapping(value = "/api/report/commit")
    public Resp<Boolean> commit2(@RequestHeader("token") String token,
                                 @RequestBody VolunteerReport param) {
        param.setUserId(token);
        return Resp.ok(param.insert());
    }
}
